"""
基础接口定义

定义了云盘SDK的基础接口和抽象类。
"""

from abc import ABC, abstractmethod
from typing import List, Dict, Any, Optional, Union, Tuple
from enum import Enum
import os


class FileType(Enum):
    """文件类型枚举"""
    FILE = "file"
    FOLDER = "folder"


class CloudFile:
    """云盘文件基类"""
    
    def __init__(self, file_id: str, name: str, size: int = 0, 
                 file_type: FileType = FileType.FILE, 
                 path: str = "", md5: str = "", 
                 create_time: Optional[str] = None, 
                 modify_time: Optional[str] = None,
                 parent_id: Optional[str] = None):
        self.file_id = file_id
        self.name = name
        self.size = size
        self.file_type = file_type
        self.path = path
        self.md5 = md5
        self.create_time = create_time
        self.modify_time = modify_time
        self.parent_id = parent_id
    
    def is_dir(self) -> bool:
        """是否为文件夹"""
        return self.file_type == FileType.FOLDER
    
    def is_file(self) -> bool:
        """是否为文件"""
        return self.file_type == FileType.FILE
    
    def __str__(self):
        return f"{self.name} ({'dir' if self.is_dir() else 'file'})"
    
    def __repr__(self):
        return f"<CloudFile id={self.file_id} name={self.name} type={self.file_type} size={self.size}>"


class UploadResult:
    """上传结果"""
    
    def __init__(self, success: bool, file_id: str = "", message: str = "", 
                 rapid_upload: bool = False):
        self.success = success
        self.file_id = file_id
        self.message = message
        self.rapid_upload = rapid_upload
    
    def __str__(self) -> str:
        status = "成功" if self.success else "失败"
        rapid_info = " (秒传)" if self.rapid_upload else ""
        return f"上传{status}{rapid_info}: {self.message}"


class DownloadResult:
    """下载结果"""
    
    def __init__(self, success: bool, message: str = "", 
                 local_path: str = ""):
        self.success = success
        self.message = message
        self.local_path = local_path
    
    def __str__(self) -> str:
        status = "成功" if self.success else "失败"
        return f"下载{status}: {self.message}"


class BaseCloudProvider(ABC):
    """云盘提供者基类"""
    
    def __init__(self, config: Dict[str, Any]):
        self.config = config
        self._session_data = {}
    
    @abstractmethod
    def login(self) -> bool:
        """
        登录云盘
        
        Returns:
            bool: 登录是否成功
        """
        pass
    
    @abstractmethod
    def logout(self) -> bool:
        """
        登出云盘
        
        Returns:
            bool: 登出是否成功
        """
        pass
    
    @abstractmethod
    def list(self, path: str = "/", recursive: bool = False) -> List[CloudFile]:
        """
        列出文件和文件夹
        
        Args:
            path: 路径，默认为根目录
            recursive: 是否递归列出子目录
            
        Returns:
            List[CloudFile]: 文件列表
        """
        pass
    
    @abstractmethod
    def upload(self, local_path: str, remote_path: str, 
               rapid_upload: bool = True) -> UploadResult:
        """
        上传文件
        
        Args:
            local_path: 本地文件路径
            remote_path: 远程文件路径
            rapid_upload: 是否尝试秒传
            
        Returns:
            UploadResult: 上传结果
        """
        pass
    
    @abstractmethod
    def download(self, remote_path: str, local_path: str) -> DownloadResult:
        """
        下载文件
        
        Args:
            remote_path: 远程文件路径
            local_path: 本地保存路径
            
        Returns:
            DownloadResult: 下载结果
        """
        pass
    
    @abstractmethod
    def delete(self, remote_path: str) -> bool:
        """
        删除文件或文件夹
        
        Args:
            remote_path: 远程文件路径
            
        Returns:
            bool: 删除是否成功
        """
        pass
    
    @abstractmethod
    def mkdir(self, path: str) -> bool:
        """
        创建文件夹
        
        Args:
            path: 文件夹路径
            
        Returns:
            bool: 创建是否成功
        """
        pass
    
    @abstractmethod
    def move(self, src_path: str, dst_path: str) -> bool:
        """
        移动文件或文件夹
        
        Args:
            src_path: 源路径
            dst_path: 目标路径
            
        Returns:
            bool: 移动是否成功
        """
        pass
    
    @abstractmethod
    def copy(self, src_path: str, dst_path: str) -> bool:
        """
        复制文件或文件夹
        
        Args:
            src_path: 源路径
            dst_path: 目标路径
            
        Returns:
            bool: 复制是否成功
        """
        pass
    
    @abstractmethod
    def rename(self, src_path: str, new_name: str) -> bool:
        """
        重命名文件或文件夹
        
        Args:
            src_path: 源路径
            new_name: 新名称
            
        Returns:
            bool: 重命名是否成功
        """
        pass
    
    @abstractmethod
    def get_file_info(self, path: str) -> Optional[CloudFile]:
        """
        获取文件信息
        
        Args:
            path: 文件路径
            
        Returns:
            Optional[CloudFile]: 文件信息，不存在返回None
        """
        pass
    
    @abstractmethod
    def get_download_url(self, path: str) -> Optional[str]:
        """
        获取文件下载链接
        
        Args:
            path: 文件路径
            
        Returns:
            Optional[str]: 下载链接，失败返回None
        """
        pass
    
    @abstractmethod
    def save_session(self, path: str) -> bool:
        """
        保存会话信息
        
        Args:
            path: 保存路径
            
        Returns:
            bool: 保存是否成功
        """
        pass
    
    @abstractmethod
    def load_session(self, path: str) -> bool:
        """
        加载会话信息
        
        Args:
            path: 会话文件路径
            
        Returns:
            bool: 加载是否成功
        """
        pass
    
    def is_rapid_upload_supported(self) -> bool:
        """
        是否支持秒传
        
        Returns:
            bool: 是否支持秒传
        """
        return False